using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
public partial class Admin_CTDonHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbDonHangEntities donHang = tbDonHangBUS.GetByID(id);
            lit_MaDonHang.Text = donHang.MaDH.ToString();

            tbKhachHangEntities khachHang = tbKhachHangBUS.GetByID(donHang.MaKH);
            lit_KhachHang.Text = khachHang.HoTen;
            lit_NgayDatHang.Text = donHang.NgayDatHang.ToString();
            lit_NgayGiaoHang.Text = donHang.NgayGiao.ToString();
            lit_NoiGiao.Text = donHang.NoiGiao;
            lit_TriGia.Text = donHang.TriGia.ToString();
            lit_ttdh.Text = donHang.MaTT.ToString();
            lit_gd.Text = donHang.MaGD.ToString();
            

            int total = 0;
            Criteria cri = new Criteria();
            cri.add(Condition.AND, tbChiTiet_DonHangEntities.tbChiTiet_DonHangColumns.MaDH, Condition.EQUAL, donHang.MaDH);
            Pager.PageSize = int.Parse(ConfigurationManager.AppSettings["pagesize"]);
            tbChiTiet_DonHangEntities.tbChiTiet_DonHangEntitiesCollection ctDonHangCollection =
                tbChiTiet_DonHangBUS.Get(cri.Criter, Pager.CurrentIndex, Pager.PageSize,
                tbChiTiet_DonHangEntities.tbChiTiet_DonHangColumns.MaCTDH.ToString(), OrderDirection.ASC.ToString(), out total);
            Repeater1.DataSource = ctDonHangCollection;
            Repeater1.DataBind();

            Pager.ItemCount = total;
            if (Pager.ItemCount <= Pager.PageSize)
                Pager.Visible = false;
            else
                Pager.Visible = true;
        }
        catch
        {
            Response.Redirect("~/Admin/DanhSachDonHang.aspx");
        }
    }

    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbChiTiet_DonHangEntities ctDonHang = (tbChiTiet_DonHangEntities)e.Item.DataItem;
            tbSanPhamEntities sp = tbSanPhamBUS.GetByID(ctDonHang.MaSP);
            if (sp != null)
            {
                Literal lit_Ten = (Literal)e.Item.FindControl("lit_Ten");
                lit_Ten.Text = sp.TenSP;
            }
            Literal lit_ThanhTien = (Literal)e.Item.FindControl("lit_ThanhTien");
            int tongTien = ctDonHang.SoLuong * ctDonHang.DonGia;
            lit_ThanhTien.Text = tongTien.ToString();
        }
    }
}
